package com.ymatou.auth.testcase;

import java.util.ArrayList;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.auth.parameter.LockUserRequestDtoBean;
import com.ymatou.auth.parameter.LoginAuthBean;
import com.ymatou.auth.service.LockUserRequestDtoCall;
import com.ymatou.auth.service.LoginAuthCall;
import com.ymatou.auth.service.UnLockUserRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.AuthCallService;
import com.ymttest.business.service.UserCallService;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
*解锁用户接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UnLockUserRequestDto{
	private static LockUserRequestDtoBean unlockuserrequestdtoBean;
	private static UnLockUserRequestDtoCall unlockuserrequestdtoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("解锁用户接口");
	}

	@Before
	public void caseUp() {
		unlockuserrequestdtoBean=new LockUserRequestDtoBean();
		unlockuserrequestdtoCall=new UnLockUserRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(unlockuserrequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UnLockUserRequestDto_001() {
		Logger.start(true,"解锁用户接口成功");
		try {			
			LockUserRequestDtoBean lockuserrequestdtoBean=new LockUserRequestDtoBean();
			LockUserRequestDtoCall lockuserrequestdtoCall=new LockUserRequestDtoCall();
			Map UserMap =UserCallService.registerByMobile();
			String ip = (String)UserMap.get("ip");
			String AccessToken = (String) UserMap.get("accesstoken");
			int UserId = (Integer) UserMap.get("userid");
			String UserName = (String) UserMap.get("username");
			ArrayList<Integer> users=new ArrayList<Integer>();
			users.add(UserId);
			Logger.comment("锁住用户");
			lockuserrequestdtoBean.setUserId(users);
			lockuserrequestdtoBean.setDescription("autotest");
			lockuserrequestdtoBean.setOperating("auto");
			lockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			lockuserrequestdtoCall.callService();	
			//登录验证
			Logger.comment("开始登陆，验证用户被锁定");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setLoginId(UserName);
			lab.setPas("123456a");
			lab.setSourceType("app");
			lab.setClientIp(ip);
			lac.setData(false,lab);
			lac.callService();
			Logger.verifyEquals("用户被锁定", lac.getMsg(), "验证登录返回消息");		
			//解锁
			Logger.comment("解锁用户");
			unlockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			unlockuserrequestdtoCall.callService();		
			//JIRACONN-3031
			Logger.verifyEquals(false,AuthCallService.signAuth(AccessToken).get("success"),"锁定解锁后用户需要重新登入,sign验证false");
			//登录验证
			Thread.sleep(1000);
			lac = new LoginAuthCall();
			Logger.comment("开始登陆，验证用户已解锁");
			lab.setLoginId(UserName);
			lab.setPas("123456a");
			lab.setSourceType("app");
			lab.setClientIp(ip);
			lac.setData(false,lab);
			lac.callService();
			Logger.verifyEquals(true, lac.getSuccess(), "验证登录成功");		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UnLockUserRequestDto_002() {
		Logger.start(true,"批量解锁用户接口成功");
		try {			
			LockUserRequestDtoBean lockuserrequestdtoBean=new LockUserRequestDtoBean();
			LockUserRequestDtoCall lockuserrequestdtoCall=new LockUserRequestDtoCall();		
			ArrayList<Integer> userIds=new ArrayList<Integer>();
			ArrayList<String> userNames=new ArrayList<String>();
			ArrayList<String> ips=new ArrayList<String>();
			Logger.comment("注册2个新用户");
			for (int i=0;i<2;i++){
				Map UserMap =UserCallService.registerByMobile();
				String AccessToken = (String) UserMap.get("accesstoken");
				int UserId = (Integer) UserMap.get("userid");
				String UserName = (String) UserMap.get("username");
				String ip = (String) UserMap.get("ip");
				userIds.add(UserId);
				userNames.add(UserName);
				ips.add(ip);
			}	
			Logger.comment("锁住注册的用户");
			lockuserrequestdtoBean.setUserId(userIds);
			lockuserrequestdtoBean.setDescription("autotest");
			lockuserrequestdtoBean.setOperating("auto");
			lockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			lockuserrequestdtoCall.callService();			
			//登录
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			Logger.comment("开始登陆，验证用户被锁定");
			for (int i=0;i<userNames.size();i++){
				lab.setLoginId(userNames.get(i));
				lab.setPas("123456a");
				lab.setClientIp(TestDataManager.getRandomIp());
				lab.setSourceType("app");//main
				lab.setDeviceId("app");
				lab.setClientIp(ips.get(i));
				lac.setData(false,lab);
				lac.callService();
				Logger.verifyEquals("用户被锁定", lac.getMsg(), "验证"+userNames.get(i)+"登录返回提示用户锁定");		
			}
			//解锁
			Logger.comment("解锁用户");
			unlockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			unlockuserrequestdtoCall.callService();			
			//登录验证
			Logger.comment("开始登陆，验证用户已解锁");
			int i=0;
			for (String name:userNames){
				lab.setLoginId(name);
				lab.setPas("123456a");
				lab.setClientIp(ips.get(i));
				lab.setSourceType("app");//main
				lab.setDeviceId("app");
				lac.setData(false,lab);
				AuthCallService.authApp(null, "app", name, "123456a", "app");
				lac.callService();
				Logger.verifyEquals(true, lac.getSuccess(), "验证"+name+"+登录成功");	
				i++;
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UnLockUserRequestDto_003() {
		Logger.start(false,"用户id为空，失败");
		try {			
			unlockuserrequestdtoBean.setDescription("autotest");
			unlockuserrequestdtoBean.setOperating("auto");
			unlockuserrequestdtoCall.setData(unlockuserrequestdtoBean);
			unlockuserrequestdtoCall.callService();			
			Logger.verifyEquals(ECode.AUTH_NOUSERID,unlockuserrequestdtoCall.getMsg(), "验证提示信息");			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UnLockUserRequestDto_004() {
		Logger.start(false,"操作为空，失败");
		try {			
			ArrayList<Integer> users = new ArrayList<Integer>();
			users.add(102);
			unlockuserrequestdtoBean.setUserId(users);
			unlockuserrequestdtoBean.setDescription("autotest");
			unlockuserrequestdtoCall.setData(unlockuserrequestdtoBean);
			unlockuserrequestdtoCall.callService();			
			Logger.verifyEquals(ECode.AUTH_NOOPERATION_DESCRIPTION,unlockuserrequestdtoCall.getMsg(), "验证提示信息");			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UnLockUserRequestDto_005() {
		Logger.start(false,"操作描述为空，失败");
		try {			
			ArrayList<Integer> users = new ArrayList<Integer>();
			users.add(102);
			unlockuserrequestdtoBean.setUserId(users);
			unlockuserrequestdtoBean.setOperating("auto");
			unlockuserrequestdtoCall.setData(unlockuserrequestdtoBean);
			unlockuserrequestdtoCall.callService();			
			Logger.verifyEquals(ECode.AUTH_NOOPERATION_DESCRIPTION,unlockuserrequestdtoCall.getMsg(), "验证提示信息");			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}